package com.nowcoder.Offer.Array;

import org.junit.Test;

import java.util.HashMap;

/**
 * 字符流第一个只出现一次的字符
 */
public class FirstOnceChar {

    HashMap<Character,Integer> hash = new HashMap<>();

    StringBuilder strb = new StringBuilder();

    public void Insert(char ch){
        hash.put(ch,hash.getOrDefault(ch,0)+1);
        strb.append(ch);
    }


    //return the first appearence once char in current stringstream
    public char FirstAppearingOnce(){
        String str = strb.toString();
        for(int i=0; i<str.length(); i++){
            if(hash.get(str.charAt(i)) == 1)
                return str.charAt(i);
        }
        return '#';
    }
    //!@$%^&*()_
    @Test
    public void test(){
        FirstOnceChar firstOnceChar = new FirstOnceChar();

//        firstOnceChar.Insert('g');
//        firstOnceChar.Insert('o');
//        System.out.println(firstOnceChar.FirstAppearingOnce());

        firstOnceChar.Insert('!');System.out.println(firstOnceChar.FirstAppearingOnce());
        firstOnceChar.Insert('@');System.out.println(firstOnceChar.FirstAppearingOnce());
        firstOnceChar.Insert('$');System.out.println(firstOnceChar.FirstAppearingOnce());
        firstOnceChar.Insert('%');System.out.println(firstOnceChar.FirstAppearingOnce());
        firstOnceChar.Insert('^');System.out.println(firstOnceChar.FirstAppearingOnce());
        firstOnceChar.Insert('&');System.out.println(firstOnceChar.FirstAppearingOnce());
        firstOnceChar.Insert('*');System.out.println(firstOnceChar.FirstAppearingOnce());
        firstOnceChar.Insert('(');System.out.println(firstOnceChar.FirstAppearingOnce());
        firstOnceChar.Insert(')');System.out.println(firstOnceChar.FirstAppearingOnce());





    }
}
